package com.ym.b2b.ws.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.jws.WebService;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.ym.b2b.order.service.WebServiceOrderService;
import com.ym.b2b.order.sqlbean.WebServiceOrder;
import com.ym.b2b.ws.base.BaseWs;
import com.ym.b2b.ws.base.ReturnModel;
import com.ym.b2b.ws.service.ForERPService;
import com.ym.b2b.wsBuyer.service.WebServiceBuyerService;
import com.ym.b2b.wsBuyer.sqlBean.WebServiceBuyer;

@Service
@WebService
public class ForERPServiceImpl extends BaseWs implements ForERPService {

	@Autowired
	private WebServiceOrderService service;
	
	@Autowired
	private WebServiceBuyerService buyerService;

	/**
	 * getOrderList
	 * 获取订单列表,默认获取已确认,待审核的订单列表
	 * @param startDate
	 * @param endDate
	 * @param status
	 * @return JSONString
	 */
	@Override
	public String getOrderList(String startDate, String endDate, String status) {
		ReturnModel returnModel = new ReturnModel();
		try {
			WebServiceOrder order = new WebServiceOrder();
			order.setBuyerCode("00040002"); // TODO 辉美测试用,写死采购商
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");// 可以方便地修改日期格式
			String today = dateFormat.format(new Date());
			order.setStartDate(StringUtils.isBlank(startDate) ? today
					: startDate);
			order.setEndDate(StringUtils.isBlank(endDate) ? today : endDate);
			order.setStatus(StringUtils.isBlank(endDate) ? "10" : status);
			returnModel.setList(service.getOrderList(order));
		} catch (Exception e) {
			returnModel.setCode("100");
			returnModel.setReasonMsg("系统异常");
			e.printStackTrace();
		}
		return JSON.toJSONString(returnModel);
	}

	/**
	 * Title: updateOrder
	 * 更新订单状态 
	 * @param orderCode
	 * @param erpOrderCode
	 * @param status
	 */
	@Override
	public String updateOrder(String orderCode, String orderCodeErp,
			String status, String oos) {
		logger.info("orderCode: " + orderCode + "orderCodeErp: " + orderCodeErp
				+ "status: " + status + "oos:" + oos);
		ReturnModel returnModel = new ReturnModel();
		try {
			WebServiceOrder order = new WebServiceOrder();
			order.setOrderCode(orderCode);
			WebServiceOrder sysOrder = service.getOrderList(order).get(0); // 获取数据库中的订单信息
			if (("-2".equals(status) || "-1".equals(status))
					&& Integer.parseInt(sysOrder.getStatus()) >= 20) {
				returnModel.setCode("100");
				returnModel.setReasonMsg("订单已付款,不可更改");
				return JSON.toJSONString(returnModel);
			} else if ("50".equals(status) && StringUtils.isBlank(orderCodeErp)) { // ERP审核通过应该回传ERPCode
				returnModel.setCode("100");
				returnModel.setReasonMsg("确认审核订单应该回传ERPCODE");
				return JSON.toJSONString(returnModel);
			} else if ("-2".equals(status) && !StringUtils.isBlank(oos)) { // 订单有缺货,ERP审核不通过
				order.setOos(oos);
				orderCodeErp = null;
				service.updateOrderItemForOos(order);
			}
			order.setOrderCodeErp(orderCodeErp);
			order.setStatus(status);
			int i = service.updateOrder(order);
			if (i > 0) {
				returnModel.setReasonMsg("操作成功");
			}
		} catch (Exception e) {
			returnModel.setCode("100");
			returnModel.setReasonMsg("系统异常");
			e.printStackTrace();
		}
		return JSON.toJSONString(returnModel);
	}

	@Override
	public String checkFlag(String orderCode) {
		logger.info("WebService[checkFlag],传入参数[" + "orderCode: " + orderCode
				+ "]");
		ReturnModel returnModel = new ReturnModel();
		try {
			String[] orderCodes = orderCode.split(",");
			for (String str : orderCodes) {
				WebServiceOrder order = new WebServiceOrder();
				order.setOrderCode(str);
				int sysOrderStatus = Integer.parseInt(service
						.getOrderList(order).get(0).getStatus()); // 获取数据库中的订单信息
				if (sysOrderStatus == 10 || sysOrderStatus == 20) {
					order.setStatus(String.valueOf(sysOrderStatus + 1));
					service.updateOrder(order);
				}
			}
			returnModel.setReasonMsg("操作成功");
		} catch (Exception e) {
			returnModel.setCode("100");
			returnModel.setReasonMsg("系统异常");
			e.printStackTrace();
		}
		return JSON.toJSONString(returnModel);
	}

	@Override
	public String addBuyer(String jsonString) {
		logger.info("WebService[addBuyer],传入参数[" + "jsonString: " + jsonString + "]");
		ReturnModel returnModel = new ReturnModel();
		try {
			WebServiceBuyer buyer = JSON.parseObject(jsonString, WebServiceBuyer.class);
			if(StringUtils.isBlank(buyer.getBuyerCode()) || StringUtils.isBlank(buyer.getChsName())){
				returnModel.setCode("100");
				returnModel.setReasonMsg("缺少必要参数");
			}
			buyerService.insertBuyer(buyer);
			returnModel.setReasonMsg("操作成功");
		} catch (Exception e) {
			returnModel.setCode("100");
			returnModel.setReasonMsg("系统异常");
			e.printStackTrace();
		}
		return JSON.toJSONString(returnModel);
	}

}